package com.sloth.controller;

import com.sloth.pojo.Address;
import com.sloth.pojo.Myorder;
import com.sloth.pojo.Product;
import com.sloth.service.MyOrderService;
import com.sloth.service.ReadyPayService;
import com.sloth.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @author lai
 * @time 2021/11/6 20:09
 */
@Controller
public class ReadyPayController {

    @Autowired
    private MyOrderService myOrderService;

    @Autowired
    private ReadyPayService readyPayService;

    @Autowired
    private SeckillService seckillService;

    @GetMapping("order/readyToPay/{newMyOrderId}")
    @ResponseBody
    public ModelAndView readPay(@PathVariable("newMyOrderId") Integer newMyOrderId){

        // 通过 订单id 查询订单编号 、 下单时间 、 收货地址信息
        Myorder myorder = myOrderService.getMyOrderByMyOrderID(newMyOrderId);
        Long orderNo = myorder.getOrderNo();  // 订单编号
        Date orderDate = myorder.getOrderDate();  // 下单时间
        Double orderPrice = myorder.getOrderPrice();  // 订单金额
        // 通过收货地址id查询收货地址信息
        Address address = readyPayService.getAddressByAddressId(myorder.getAddressId());
        // 返回订单中所有商品名称
        List<Product> productList = readyPayService.getProductListByOrderId(myorder.getOrderId());
        // 将商品名称拼起来，返回给页面
        StringBuffer stringBuffer = new StringBuffer();
        int len = 0;
        // 遍历商品信息，把商品名称拼接起来
        for (Product product : productList){
            len ++;
            stringBuffer.append(product.getProDesc());
            if (len < productList.size()){
                stringBuffer.append(",");
            }
        }


        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("orderId",myorder.getOrderId());
        modelAndView.addObject("orderNo",orderNo);
        modelAndView.addObject("orderDate",orderDate);
        modelAndView.addObject("orderPrice",orderPrice);
        modelAndView.addObject("address",address);
        modelAndView.addObject("stringBuffer",stringBuffer);

        modelAndView.setViewName("/order/ready_pay");

        return modelAndView;
    }

    // 传递的是 订单id 和 选择的 收货地址id
    @GetMapping("order/seckillReadyToPay/{seckillMyOrderId}")
    @ResponseBody
    public ModelAndView SeckillReadPay(@PathVariable("seckillMyOrderId") Integer seckillMyOrderId){

        // 通过 订单id 查询订单编号 、 下单时间 、 收货地址信息，此时的订单id 是修改过的收货地址
        Myorder myorder = myOrderService.getMyOrderByMyOrderID(seckillMyOrderId);
        Long orderNo = myorder.getOrderNo();  // 订单编号
        Date orderDate = myorder.getOrderDate();  // 下单时间
        Double orderPrice = myorder.getOrderPrice();  // 订单金额
        // 通过收货地址id查询收货地址信息
        Address address = readyPayService.getAddressByAddressId(myorder.getAddressId());
        // 返回订单中所有商品名称
        Product product = seckillService.getProductNameBySeckillOrderId(seckillMyOrderId);
        // 获得商品名称
        String proDesc = product.getProDesc();

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("orderId",myorder.getOrderId());
        modelAndView.addObject("orderNo",orderNo);
        modelAndView.addObject("orderDate",orderDate);
        modelAndView.addObject("orderPrice",orderPrice);
        modelAndView.addObject("address",address);
        modelAndView.addObject("stringBuffer",proDesc);

        modelAndView.setViewName("/order/seckill_ready_pay");

        return modelAndView;
    }

}